﻿using System.Collections;
using System.Collections.Generic;
using com.guojin.mj.net.message.game;
using UnityEngine;
using com.guojin.core.io.message;

public class BetPageController : MonoBehaviour
{
    private static BetPageController instance;
    public static BetPageController GetInstance()
    {
        return instance;
    }
    void Awake()
    {
        instance = this;
    }

    public int betType;
    public int locationIndex;
    private int betOrCallScore;
    private BetLocationIndexRet blir;

    public void OnReceiveMsg1211(string msg, Message data)
    {
        blir = (BetLocationIndexRet)data;
        SetBetInfo(blir);
        StartCoroutine(WaitDealCardEnd());
    }

    public void SetBetInfo(BetLocationIndexRet blir)
    {
        betType = blir.betType;
        locationIndex = blir.locationIndex;
        betOrCallScore = blir.betOrCallScore;
    }

    IEnumerator WaitDealCardEnd()
    {
        yield return new WaitUntil(() => GameRoomPage.GetInstance().ShowDealPanel == false);
        ChangeUserBetState();
        CheckShowBetElement(blir);
    }

    private void ChangeUserBetState()
    {
        if (betType == 1)
        {
            GameRoomModel.GetInstance().playerDic[locationIndex].BetState = 1;
        }
        else if (betType == 2)
        {
            GameRoomModel.GetInstance().playerDic[locationIndex].BetState = 3;
        }
    }

    public void CheckShowBetElement(BetLocationIndexRet blir)
    {
        if (blir.locationIndex == GameData.GetInstance().locationIndex)
        {
            ShowBetPage();
        }
        ShowBetSelect(blir.locationIndex);
        ShowDownTime();
    }

    private void ShowBetPage()
    {
        UIManager.ChangeUI(UIManager.PageState.BetPage, (GameObject obj) =>
        {
            obj.GetComponent<BetPage>().InformationSetting();
            obj.GetComponent<BetPage>().betOrCarllNum = betOrCallScore;
            obj.GetComponent<BetPage>().ShowPageType(betType);
        });
    }

    private void ShowBetSelect(int locationIndex)
    {
        GameRoomModel.GetInstance().playerDic[locationIndex].IsSelect = true;
    }

    private void ShowDownTime()
    {
        GameRoomModel.GetInstance().playerDic[locationIndex].SetDownTime(true, GameRoomModel.GetInstance().TrusteeshipTime);
    }
}
